const MouseEventType ={
    CLICK: Cesium.ScreenSpaceEventType.LEFT_CLICK,
    RIGHT: Cesium.ScreenSpaceEventType.RIGHT_CLICK,
    MOUSE_MOVE: Cesium.ScreenSpaceEventType.MOUSE_MOVE
}
export default class EventsManager{
    constructor(viewer){
        this._viewer =viewer;
        this._cache ={};
        this._initMouseEvent()
    }
    //注册事件
    on(type, callback, context){
        let event =this.getEvent(type)
        let removeCallback =undefined;
        if(event && callback){
            removeCallback =event.addEventListener(callback,context||this)
        }
        return removeCallback
    }
    //接除事件
    off(type, callback, context){
        let event =this.getEvent(type)
        let removed =false;
        if(event && callback){
            removed =event.removeEventListener(callback, context||this)
        }
        removed;
    }

    _initMouseEvent(){
        // this._viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
        let handler =new Cesium.ScreenSpaceEventHandler(this._viewer.canvas);
        Object.keys(MouseEventType).forEach(key =>{
            let type =MouseEventType[key];
            //创建事件
            this._cache[type] =new Cesium.Event();
            handler.setInputAction(movement => {
                let info;
                if(type ==MouseEventType['MOUSE_MOVE']){
                    info =this._getMouseInfo(movement.endPosition);
                }else{
                    info =this._getMouseInfo(movement.position)
                }
                //触发各种鼠标事件后发射信号
                this._cache[type].raiseEvent(info); 
            }, type)
        })
    }
    //获取鼠标位置的点信息
    _getMouseInfo(position){
        let scene =this._viewer.scene;
        let target =scene.pick(position);
        let cartesian =undefined;
        let wgs84SurfacePosition =undefined;
        let wgs84Position =undefined;
        let surfaceCartesian =undefined;
        //笛卡尔坐标
        if(scene.pickPositionSupported){
            cartesian =scene.pickPosition(position);
        }
        //经纬度坐标
        if(cartesian){

            let c =Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian)
            if(c){
                wgs84Position ={
                    lng: Cesium.Math.toDegrees(c.longitude),
                    lat: Cesium.Math.toDegrees(c.latitude),
                    alt: c.height
                }
            }
        }
        //表面笛卡尔--surfaceCartesian
        //如果不是用的ellipsoidTerrainProvider
        if(scene.mode ===Cesium.SceneMode.SCENE3D && !(this._viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider)){
            let ray =scene.camera.getPickRay(position)
            surfaceCartesian =scene.globe.pick(ray,scene)
        }else{
            surfaceCartesian =scene.camera.pickEllipsoid(position, Cesium.Ellipsoid.WGS84)
        }

        //表面经纬度--wgs84SurfacePosition
        if(surfaceCartesian){
            let c =Cesium.Ellipsoid.WGS84.cartesianToCartographic(surfaceCartesian);
            if(c){
                wgs84SurfacePosition ={
                    lng: Cesium.Math.toDegrees(c.longitude),
                    lat: Cesium.Math.toDegrees(c.latitude),
                    alt: c.height
                }
            }
        }


        
        
        //
        return{
            target: target,
            //窗口
            windowPosition: position,
            //笛卡尔
            position: cartesian,
            //经纬度
            wgs84Position: wgs84Position,
            //表面笛卡尔
            surfacePosition: surfaceCartesian,
            //表面经纬度
            wgs84SurfacePosition: wgs84SurfacePosition
        }
    }

    getEvent(type){
        return this._cache[type] || undefined
    }
}